var app = getApp()
const toast = require('../../utils/toast.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    condition: true
  },
  toast: toast,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**  
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindBaidu: function(e) {
    var inputValue = e.detail.value;
    console.log(inputValue) 
    var that = this;
    if (inputValue && typeof inputValue != 'undefined') {
      this.connectNetForData(inputValue);
    } else {
      that.setData({
        baiduList: []
      })
    }

  },

  connectNetForData(inputValue) {
    var that = this;
    console.log(inputValue)
    console.log(app.baseNet + "appPullNewActivity/baiduQuery?query=" + inputValue)
    wx.request({
      url: app.baseNet + "appPullNewActivity/baiduQuery?query=" + encodeURI(inputValue),
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
        console.log(res.statusCode)
        if (res.statusCode == 200) {
          var search = res.data.lendReco;
          var baidu = search.baiduCoordinate;
          that.landlord = search.landlord;
          that.roomTitle = search.roomInfoTitle;
          if (baidu.length == 0) {
            that.setData({
              condition: false
            })
          } else {
            that.setData({
              condition: true
            })
            if (baidu != null && baidu.length > 0) {
              that.setData({
                baiduVisible: false,
                baiduList: baidu,
                condition: true
              })
            } else {
              that.setData({
                baiduVisible: true
              })
            }
          }
        } else {
          toast.showSystemAbnormalToast()
        }

      }
    })
  },

  clear: function(e) {
    console.log(e)
    this.setData({
      search_input: "",
      baiduList: []
    })
  },
  skipToRoomList: function(e) {
    console.log(e)
    var index = e.currentTarget.dataset.id
    var list = this.data.baiduList
    var latitude = list[index].lat
    var longtitude = list[index].lng
    var destination = list[index].name

    var citys = list[index].cityDistrict.split('-')
    var city = ''
    if (citys[0].indexOf('市')) {
      city = citys[0].split('市')[0]
    } else {
      city = citys[0]
    }

    var pages = getCurrentPages()
    var curPage = pages[pages.length - 1]
    var homePage = pages[pages.length - 3]

    homePage.setData({
      destinationCity: destination,
      areaCode: '',
      cityName: '',
      latitude: latitude,
      longitude: longtitude,
      cityName: city,
      bgColor: "rgb(52,229,234)",
      color: "rgb(51,51,51)"
    })
    wx.navigateBack({
      delta: 2
    })
  }

})